
function prePagination(prePageNo, totalPage, maxCountOfPagination, uri) {
	if (prePageNo == 0) {
		return;
	}
	var startPage, endPage;
	if ((prePageNo - maxCountOfPagination) < 0) {
		startPage = 1;
	} else {
		startPage = prePageNo - maxCountOfPagination + 1;
	}
	// alert(prePageNo + " - " + startPage);
	endPage = startPage + maxCountOfPagination;
	var ulHTML = '<li><a href="###" onclick="prePagination(' + (startPage - 1)
			+ ',' + totalPage + ',' + maxCountOfPagination + ',\'' + uri
			+ '\')">Prev</a>';
	for ( var i = startPage; i < endPage; i++) {
		var newURI = setPageForURI(uri, i);
		ulHTML += ('<li><a href="' + newURI + '">' + i + '</a></li>');
	}
	ulHTML += ('<li><a href="###" onclick="nextPagination(' + (endPage) + ','
			+ totalPage + ',' + maxCountOfPagination + ',\'' + uri + '\')">Next</a></li>');
	$("#pageUL").html(ulHTML);
}

function nextPagination(nextPageNo, totalPage, maxCountOfPagination, uri) {
	if (nextPageNo > totalPage) {
		return;
	}
	var startPage, endPage;
	if ((nextPageNo + maxCountOfPagination) > totalPage + 1) {
		startPage = totalPage - maxCountOfPagination + 1;
	} else {
		startPage = nextPageNo;
	}
	endPage = startPage + maxCountOfPagination;
	// alert(endPage + " " + totalPage);
	var ulHTML = '<li><a href="###" onclick="prePagination(' + (startPage - 1)
			+ ',' + totalPage + ',' + maxCountOfPagination + ',\'' + uri
			+ '\')">Prev</a>';
	for ( var i = startPage; i < endPage; i++) {
		var newURI = setPageForURI(uri, i);
		ulHTML += ('<li><a href="' + newURI + '">' + i + '</a></li>');
	}
	ulHTML += ('<li><a href="###" onclick="nextPagination(' + endPage + ','
			+ totalPage + ',' + maxCountOfPagination + ',\'' + uri + '\')">Next</a></li>');
	$("#pageUL").html(ulHTML);
}

function setPageForURI(uri, page) {
	var newURI;
	var index = uri.indexOf("p=");
	if (index != -1) {
		var s1 = uri.substring(0, index);
		var s2 = uri.substring(index, uri.length);
		var s3 = '';
		index = s2.indexOf("&");
		if (index != -1) {
			s3 = s2.substring(index, s2.length);
		}
		if (s3 != '') {
			newURI = s1 + "p=" + page + s3;
		} else {
			newURI = s1 + "p=" + page;
		}
	} else {
		if (uri.indexOf("?") != -1) {
			newURI = uri + "&p=" + page;
		} else {
			newURI = uri + "?p=" + page;
		}
	}
	return newURI;
}

$(document).ready(function() {
	// alert($("#btnBack"));
	if ($("#btnBack") != null) {
		$("#btnBack").click(function() {
			history.go(-1);
			return false;
		});
	}
});

function query(uri) {
	var q = $("#q").val();
	location.href = uri + encodeURIComponent(encodeURIComponent(q));
}

function back() {
	history.go(-1);
	return false;
}

function reload(result) {
	location.reload();
}

function callAjax(url, params, completeMethod, loadingMessage) {
	if (loadingMessage != null) {
		if (loadingMessage == "") {
			loadingMessage = "<img src='../assets/img/loading.gif'> 提交中，请稍等...";
		}
		showLoading(loadingMessage);
	}
	params += "&callType=1";
	$.ajax({
		type : "POST",
		dataType : "json",
		url : url,
		data : params,
		// async:false,
		beforeSend : function(xmlHttpRequest) {
			xmlHttpRequest.setRequestHeader("call-type", "1");
		},
		success : function(data) {
			// showSucc(data);
			if (data.doneCode == 1) {
				alert("对不起,您的登录失效,请重新登录");
				location.href = "/login.jsp?url="+encodeURIComponent(location.href);
			} else {
				if (completeMethod != null) {
					var _method = eval(completeMethod);
					_method(data);
				} else {
					showSucc();
				}
			}
		},
		error : function(xmlHttpRequest, textStatus, errorThrown) {
			var str;
			if (xmlHttpRequest.status == 404) {
				str = "Not Found " + url;
			} else {
				str = xmlHttpRequest.responseText;
				str = str.substring(str.indexOf("<pre>") + 5, str.length);
				str = str.substring(0, str.indexOf("</pre>"));
			}
			showErr(str);
		}
	});
	return false;
}

function showLoading(loadingMessage) {
	$("#alert").html(loadingMessage);
	$("#alert").attr('class', "alert alert-info");
	$("#alert").show();
}

function showSucc() {
	$("#alert").html("提交成功");
	$("#alert").attr('class', "alert alert-success");
	// $("#alert").attr('class', "hidden");
	$("#alert").show();
	setTimeout("hiddenStatus()", 3000);
}

function showInfo(str) {
	$("#alert").html(str);
	$("#alert").attr('class', "alert alert-success");
	// $("#alert").attr('class', "hidden");
	$("#alert").show();
	setTimeout("hiddenStatus()", 3000);
}

function showErr(msg) {
	// data-dismiss='alert'
	$("#alert")
			.html(
					"<button type='button' class='close' onclick='hiddenStatus()'>&times;</button> 提交失败 ，失败原因：<br>"
							+ msg);
	$("#alert").attr('class', "alert alert-error");
	// $("#alert").attr('class', "hidden");
	$("#alert").show();
}

function hiddenStatus() {
	$("#alert").attr('class', "hidden");
	$("#alert").hide();
}

function showAlert(msg) {
	// data-dismiss='alert'
	$("#alert")
			.html(
					"<button type='button' class='close' onclick='hiddenStatus()'>&times;</button> "
							+ msg);
	$("#alert").attr('class', "alert alert-error");
	// $("#alert").attr('class', "hidden");
	$("#alert").show();
}

function confirmModal(headStr, bodyStr, callbackMethod, params) {
	$('#modal').confirmModal({
		heading : headStr,
		body : bodyStr,
		callback : function() {
			if (callbackMethod != null) {
				var _method = eval(callbackMethod);
				_method(params);
			}
		}
	});
}